<?php

use think\migration\Migrator;
use think\migration\db\Column;

/**
 * 盲盒功能数据库迁移
 * 创建时间: 2025-10-27
 */
class BlindBoxTables extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        // 1. 创建盲盒主表
        $this->createBlindBoxTable();
        
        // 2. 创建盲盒奖品池表
        $this->createBlindBoxPrizeTable();
        
        // 3. 创建盲盒开奖记录表
        $this->createBlindBoxRecordTable();
    }
    
    /**
     * 创建盲盒主表
     */
    private function createBlindBoxTable()
    {
        $table = $this->table('sh_blind_box', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '盲盒主表'
        ]);
        
        $table->addColumn('uniacid', 'integer', [
                'default' => 0,
                'comment' => '站点ID'
            ])
            ->addColumn('name', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '盲盒名称'
            ])
            ->addColumn('description', 'text', [
                'null' => true,
                'comment' => '盲盒描述'
            ])
            ->addColumn('image', 'string', [
                'limit' => 500,
                'default' => '',
                'comment' => '盲盒封面图'
            ])
            ->addColumn('price', 'decimal', [
                'precision' => 10,
                'scale' => 2,
                'default' => 0.00,
                'comment' => '盲盒价格'
            ])
            ->addColumn('stock', 'integer', [
                'default' => -1,
                'comment' => '库存数量，-1表示不限制'
            ])
            ->addColumn('sold', 'integer', [
                'default' => 0,
                'comment' => '已售数量'
            ])
            ->addColumn('limit_per_user', 'integer', [
                'default' => 0,
                'comment' => '每人限购数量，0表示不限制'
            ])
            ->addColumn('start_time', 'datetime', [
                'null' => true,
                'comment' => '开始时间'
            ])
            ->addColumn('end_time', 'datetime', [
                'null' => true,
                'comment' => '结束时间'
            ])
            ->addColumn('status', 'boolean', [
                'default' => 1,
                'comment' => '状态：0-下架，1-上架'
            ])
            ->addColumn('sort_order', 'integer', [
                'default' => 0,
                'comment' => '排序'
            ])
            ->addColumn('is_deleted', 'boolean', [
                'default' => 0,
                'comment' => '是否删除：0-未删除，1-已删除'
            ])
            ->addColumn('create_time', 'datetime', [
                'null' => true,
                'comment' => '创建时间'
            ])
            ->addColumn('update_time', 'datetime', [
                'null' => true,
                'comment' => '更新时间'
            ])
            ->addIndex(['uniacid'], ['name' => 'idx_uniacid'])
            ->addIndex(['status'], ['name' => 'idx_status'])
            ->addIndex(['is_deleted'], ['name' => 'idx_is_deleted'])
            ->addIndex(['start_time', 'end_time'], ['name' => 'idx_time_range'])
            ->create();
    }
    
    /**
     * 创建盲盒奖品池表
     */
    private function createBlindBoxPrizeTable()
    {
        $table = $this->table('sh_blind_box_prize', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '盲盒奖品池表'
        ]);
        
        $table->addColumn('blind_box_id', 'integer', [
                'comment' => '盲盒ID'
            ])
            ->addColumn('prize_type', 'string', [
                'limit' => 20,
                'default' => 'goods',
                'comment' => '奖品类型：goods-商品，coupon-优惠券，points-积分'
            ])
            ->addColumn('prize_id', 'integer', [
                'default' => 0,
                'comment' => '奖品关联ID（商品ID/优惠券ID等）'
            ])
            ->addColumn('prize_name', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '奖品名称'
            ])
            ->addColumn('prize_image', 'string', [
                'limit' => 500,
                'default' => '',
                'comment' => '奖品图片'
            ])
            ->addColumn('prize_value', 'string', [
                'limit' => 100,
                'default' => '',
                'comment' => '奖品价值（积分数/优惠金额等）'
            ])
            ->addColumn('probability', 'decimal', [
                'precision' => 5,
                'scale' => 2,
                'default' => 0.00,
                'comment' => '中奖概率（百分比，如10.50表示10.5%）'
            ])
            ->addColumn('total_stock', 'integer', [
                'default' => -1,
                'comment' => '奖品总库存，-1表示不限制'
            ])
            ->addColumn('remain_stock', 'integer', [
                'default' => -1,
                'comment' => '剩余库存，-1表示不限制'
            ])
            ->addColumn('prize_level', 'string', [
                'limit' => 20,
                'default' => '',
                'comment' => '奖品等级：SSR/SR/R/N等'
            ])
            ->addColumn('sort_order', 'integer', [
                'default' => 0,
                'comment' => '排序'
            ])
            ->addColumn('status', 'boolean', [
                'default' => 1,
                'comment' => '状态：0-禁用，1-启用'
            ])
            ->addColumn('create_time', 'datetime', [
                'null' => true,
                'comment' => '创建时间'
            ])
            ->addIndex(['blind_box_id'], ['name' => 'idx_blind_box_id'])
            ->addIndex(['prize_type'], ['name' => 'idx_prize_type'])
            ->addIndex(['status', 'remain_stock'], ['name' => 'idx_status_stock'])
            ->create();
    }
    
    /**
     * 创建盲盒开奖记录表
     */
    private function createBlindBoxRecordTable()
    {
        $table = $this->table('sh_blind_box_record', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '盲盒开奖记录表'
        ]);
        
        $table->addColumn('uniacid', 'integer', [
                'default' => 0,
                'comment' => '站点ID'
            ])
            ->addColumn('blind_box_id', 'integer', [
                'comment' => '盲盒ID'
            ])
            ->addColumn('blind_box_name', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '盲盒名称（冗余字段）'
            ])
            ->addColumn('customer_id', 'integer', [
                'comment' => '用户ID'
            ])
            ->addColumn('order_id', 'integer', [
                'null' => true,
                'comment' => '订单ID'
            ])
            ->addColumn('prize_id', 'integer', [
                'comment' => '中奖奖品ID'
            ])
            ->addColumn('prize_type', 'string', [
                'limit' => 20,
                'default' => '',
                'comment' => '奖品类型'
            ])
            ->addColumn('prize_name', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '奖品名称'
            ])
            ->addColumn('prize_image', 'string', [
                'limit' => 500,
                'default' => '',
                'comment' => '奖品图片'
            ])
            ->addColumn('prize_value', 'string', [
                'limit' => 100,
                'default' => '',
                'comment' => '奖品价值'
            ])
            ->addColumn('prize_level', 'string', [
                'limit' => 20,
                'default' => '',
                'comment' => '奖品等级'
            ])
            ->addColumn('is_delivered', 'boolean', [
                'default' => 0,
                'comment' => '是否已发放：0-未发放，1-已发放'
            ])
            ->addColumn('delivered_time', 'datetime', [
                'null' => true,
                'comment' => '发放时间'
            ])
            ->addColumn('remark', 'string', [
                'limit' => 500,
                'default' => '',
                'comment' => '备注'
            ])
            ->addColumn('create_time', 'datetime', [
                'null' => true,
                'comment' => '抽取时间'
            ])
            ->addIndex(['uniacid'], ['name' => 'idx_uniacid'])
            ->addIndex(['blind_box_id'], ['name' => 'idx_blind_box_id'])
            ->addIndex(['customer_id'], ['name' => 'idx_customer_id'])
            ->addIndex(['order_id'], ['name' => 'idx_order_id'])
            ->addIndex(['create_time'], ['name' => 'idx_create_time'])
            ->create();
    }
}
